#include <stdio.h>
#include <stdlib.h>
#include <assert.h>

#include "list_int.h"


LinkedList linked_list_create(ListElementType data, LinkedList next) {
	LinkedList list;
	list = (LinkedList)malloc(sizeof(_LinkedList));
	assert(list);
	list->data = data;
	list->next = next;
	return list;
}

LinkedList linked_list_free(LinkedList list) {
	LinkedList tmp;
	while (list) {
		tmp = list->next;
		free(list);
		list = tmp;
	}
	return NULL;
}

LinkedList linked_list_push(LinkedList list, ListElementType x) {
	assert(list);
	LinkedList prev = list;
	while (prev->next)
		prev = prev->next;
	prev->next = linked_list_create(x, NULL);
	return list;
}

LinkedList linked_list_head(LinkedList list) {
	assert(list);
	return list->next;
}

int linked_list_is_empty(LinkedList list) {
	assert(list);
	return list->next == NULL;
}

void print_list(LinkedList list) {
	LinkedList head = list->next;
	int n = 0;
	while (head)
	{
		if (n)printf(" ");
		else ++n;
		printf("%d", head->data);
		head = head->next;
	}
	if (list->next == NULL)
		printf("NULL");
	printf("\n");
}